package hasmap.test1;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class HasMapTest {
    public static void main(String[] args) {
        HashMap<Student,String> hashMap = new HashMap<>();

        Student student1 = new Student("张三",23);
        Student student2 = new Student("李四",24);
        Student student3 = new Student("王五",25);

        hashMap.put(student1,"江苏");
        hashMap.put(student2,"杭州");
        hashMap.put(student3,"广州");

        //遍历
//        Set<Student> keys = hashMap.keySet();
//        for (Student key: keys) {
//            String value = hashMap.get(key);
//            System.out.println(key + "=" + value);
//        }

        //lambda及简化
        hashMap.forEach(new BiConsumer<Student, String>() {
            @Override
            public void accept(Student key, String value) {
                System.out.println(key + "=" + value);
            }
        });

        hashMap.forEach((key, value) -> System.out.println(key + "=" + value)
        );

        //自动生成左边 alt+v
        Set<Map.Entry<Student, String>> entries = hashMap.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "=" + value);
        }
    }

}
